package net.sf.nwn.jme3;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import com.jme3.asset.AssetManager;
import com.jme3.export.JmeExporter;
import com.jme3.export.JmeImporter;
import com.jme3.export.Savable;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;

public class SkinmeshGeomNode extends TrimeshGeomNode implements Savable {
	
	public static final String SKIN_INFO = "NWN_SKIN_INFO";
	

	public static class VertexWeight {
		String boneName;
		float weight;

		public VertexWeight(String boneName, float weight) {
			this.boneName = boneName;
			this.weight = weight;
		}
	}

	private Set<String> rootBones = new TreeSet<String>();

	public SkinmeshGeomNode(GeomNode parent) {
		super(parent);
		vertexWeights = new ArrayList<VertexWeight[]>();
	}

	public void addWeights(VertexWeight[] weights) {
		vertexWeights.add(weights);
		for ( VertexWeight vw : weights ) {
			rootBones.add(vw.boneName);
		}
	}
	
	public List<VertexWeight[]> getVertexWeights() {
		return vertexWeights;
	}
	
	public Set<String> getRootBones() {
		return rootBones;
	}
	
	@Override
	public Node createSingleTG(AssetManager base, ShadingType metallic) {
		
		Node node = super.createSingleTG(base, metallic);
		
		List<Spatial> nodeChildren = node.getChildren();
		if ( nodeChildren.size() > 0 ) {
			Geometry geom = (Geometry) nodeChildren.get(0);
			geom.setUserData(SKIN_INFO, this);
			
		}
		
		return node;
	}

	@Override
	public void write(JmeExporter ex) throws IOException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void read(JmeImporter im) throws IOException {
		// TODO Auto-generated method stub
		
	}
	
	public ArrayList<Vector3f> getVerts() {
		return verts;
	}

	

}
